home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / 289_01.zip / OTHELLO.C < prev    next >
Text File  |  1993-04-26  |  6KB  |  221 lines

  1. /*-----------------------------------------------------------------------------
  2. OTHELLO.C
  3.  
  4. This file contains the C main function.
  5.  
  6. Revision History
  7. ----------------
  8. Jon Ward    10 Dec 1988    Initial Revision
  9. Jon Ward     2 Jan 1989    Added the game playing loop
  10. Jon Ward     7 Jan 1989    Added lookahead to the end of game for the last
  11.                   BT_MAX_DEPTH moves.
  12. Jon Ward     8 Jan 1989    Changed lookahead level to 3 until the last 30
  13.                   moves of the game where it becomes 5.
  14. Jon Ward     9 Jan 1989    Added input of who plays first and what color.
  15. Jon Ward    29 Jan 1989    Toupperd Y/N and X/O and added missing ')'.
  16. -----------------------------------------------------------------------------*/
  17.  
  18. #include <conio.h>
  19. #include <ctype.h>
  20. #include <stdio.h>
  21. #include "othello.h"
  22.  
  23.  
  24. /*-----------------------------------------------------------------------------
  25.                         Global Variable Declarations
  26. -----------------------------------------------------------------------------*/
  27.  
  28. board_type curnt_bd;        /* current state of the board */
  29. key_type curnt_top_limb;    /* key for the board in the DB */
  30.  
  31.  
  32. /*-----------------------------------------------------------------------------
  33.                Local Function Prototypes
  34. -----------------------------------------------------------------------------*/
  35.  
  36. STATIC void play_othello (unsigned char player);
  37.  
  38.  
  39. /*-----------------------------------------------------------------------------
  40. -----------------------------------------------------------------------------*/
  41.  
  42. void main ()
  43. {
  44. unsigned char first_player;
  45. unsigned char x_player;
  46. unsigned char o_player;
  47.  
  48.  
  49. /*-----------------------------------------------
  50. Initialize all sections of the program.
  51. -----------------------------------------------*/
  52.  
  53. db_init ();            /* init database manager */
  54. disp_init ();            /* init display routines */
  55.  
  56. printf ("OTHELLO Playing Program\n");
  57. printf ("Written by Jonathan Ward and Gary Culp\n\n");
  58.  
  59.  
  60. /*-----------------------------------------------
  61. Find out who plays first.
  62. -----------------------------------------------*/
  63.  
  64.   {
  65.   register int input;
  66.  
  67.   printf ("Do you want to play first? (Y/N)\n");
  68.   
  69.   do
  70.     {
  71.     input = getch ();
  72.     input = toupper (input);
  73.     }
  74.   while (input != 'Y' && input != 'N');
  75.  
  76.   printf ("%c\n", input);
  77.   first_player = (unsigned char) ((input == 'Y') ? THEM_PIECE : US_PIECE);
  78.  
  79.   printf ("Do you want to be X or O? (X/O)\n");
  80.  
  81.   do
  82.     {
  83.     input = getch ();
  84.     input = toupper (input);
  85.     }
  86.   while (input != 'X' && input != 'O');
  87.  
  88.   printf ("%c\n", input);
  89.  
  90.   if (input == 'X')
  91.     {
  92.     x_player = THEM_PIECE;
  93.     o_player = US_PIECE;
  94.     }
  95.   else
  96.     {
  97.     o_player = THEM_PIECE;
  98.     x_player = US_PIECE;
  99.     }
  100.  
  101.   us_char = (char) ((input == 'X') ? 'O' : 'X');
  102.   them_char = (char) input;  
  103.   }
  104.  
  105.  
  106. /*-----------------------------------------------
  107. Now, setup the initial board configuration and
  108. current top limb var.  Put the initial board in
  109. the database.
  110. -----------------------------------------------*/
  111.  
  112. init_borders (&curnt_bd);
  113.  
  114.   {
  115.   register int r;
  116.   register int c;
  117.  
  118.   for (r = 1; r < 9; r++)
  119.     for (c = 1; c < 9; c++)
  120.       curnt_bd.board [r][c] = NO_PIECE;
  121.   }
  122.  
  123.  
  124. /*-----------------------------------------------
  125. Initialize the starting board and put it in the
  126. database.
  127. -----------------------------------------------*/
  128.  
  129. curnt_bd.board [4][4] = curnt_bd.board [5][5] = x_player;
  130. curnt_bd.board [4][5] = curnt_bd.board [5][4] = o_player;
  131.  
  132. curnt_top_limb = db_add_child (NO_KEY, NO_MOVE_MASK, &curnt_bd, 0);
  133.  
  134.  
  135. /*-----------------------------------------------
  136. Display the current board and begin playing the
  137. game.
  138. -----------------------------------------------*/
  139.  
  140. disp_board (&curnt_bd);
  141. play_othello (first_player);    /* play the game */
  142.  
  143. db_kill ();            /* free database allocated ram */
  144. }
  145.  
  146.  
  147. /*-----------------------------------------------------------------------------
  148. -----------------------------------------------------------------------------*/
  149.  
  150. STATIC void play_othello (unsigned char player)
  151. {
  152. register int tree_depth;
  153. move_type our_move;
  154. key_type our_move_key;
  155. register int plays_left;
  156.  
  157. player = (unsigned char) ((player & US_PIECE) ? US_PIECE : THEM_PIECE);
  158.  
  159. do
  160.   {
  161.   init_input_vars ();        /* clear input vars */
  162.  
  163. /*------------------------------------------------
  164. Determine how deep we look ahead depending on how
  165. many pieces are left on the board.
  166. ------------------------------------------------*/
  167.  
  168.   plays_left = piece_count (&curnt_bd, NO_PIECE);
  169.  
  170.   if (plays_left <= BT_MAX_DEPTH)
  171.     tree_depth = BT_MAX_DEPTH;
  172.   else if (plays_left <= 30)
  173.     tree_depth = 5;
  174.   else
  175.     tree_depth = 3;
  176.  
  177.  
  178. /*------------------------------------------------
  179. Attempt to build the board to tree_depth levels.
  180. ------------------------------------------------*/
  181.  
  182.   tree_depth = build_tree (tree_depth, player);
  183.  
  184. #if 0
  185.   dumptree(curnt_top_limb);    /***/
  186. #endif
  187.  
  188.   our_move_key = find_best_move (curnt_top_limb, tree_depth);
  189.   our_move = (db_retrieve_limb (our_move_key))->move & ~BOARD_MASK;
  190.  
  191.   if (our_move & NO_MOVE_MASK)
  192.     {
  193.     disp_player_cant_move (US_PIECE);
  194.     disp_board (&curnt_bd);
  195.     }
  196.   else
  197.     {
  198.     verify_move_and_update_board (our_move, US_PIECE);
  199.     }
  200.  
  201. #if 0        /*** change 0 to print lookahead level ***/
  202.   printf ("\nMinimax maximum = %d  ", last_max_score);
  203.   printf ("Lookahead Level = %d\n", tree_depth);
  204. #endif
  205.  
  206.   update_tree_after_our_move (our_move_key);
  207.  
  208.   player = THEM_PIECE;
  209.   }
  210. while (who_can_move (&curnt_bd));
  211.  
  212. disp_pieces (piece_count (&curnt_bd, US_PIECE),
  213.          piece_count (&curnt_bd, THEM_PIECE));
  214. }
  215.  
  216.  
  217. /*-----------------------------------------------------------------------------
  218. -----------------------------------------------------------------------------*/
  219.  
  220.  
  221.